package com.lv.common.utils;

import android.util.Base64;

import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

/**
 * DES加密工具类
 */
public class DesUtil {
    public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
    public static final String KEY = "suyiyong";

    /**
     * 加密
     *
     * @param data
     * @return
     */
    public static String encode(String data) {
        String result = encode(data.getBytes()).trim();
        if (result.contains("+")) {
            result = result.replaceAll("\\+", "%2B");
        }
        if (result.contains("/")) {
            result = result.replaceAll("/", "%2F");
        }
        return result;
    }

    /**
     * DES算法，加密
     *
     * @param data 待加密字符串
     * @return 加密后的字节数组，一般结合Base64编码使用
     * <p>
     * 异常
     */
    private static String encode(byte[] data) {
        try {
            DESKeySpec dks = new DESKeySpec(KEY.getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            // key的长度不能够小于8位字节
            Key secretKey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
            IvParameterSpec iv = new IvParameterSpec("dunmoxin".getBytes());
            AlgorithmParameterSpec paramSpec = iv;
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
            byte[] bytes = cipher.doFinal(data);
            return Base64.encodeToString(bytes, 0).trim();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * DES算法，解密
     *
     * @param data 待解密字符串
     * @return 解密后的字节数组
     * @throws Exception 异常
     */
    private static byte[] decode(byte[] data) throws Exception {
        try {
            DESKeySpec dks = new DESKeySpec(KEY.getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            // key的长度不能够小于8位字节
            Key secretKey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
            IvParameterSpec iv = new IvParameterSpec("dunmoxin".getBytes());
            AlgorithmParameterSpec paramSpec = iv;
            cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
            return cipher.doFinal(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密.
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String decode(String data) {

        if (data.contains("%2B")) {
            data = data.replaceAll("%2B", "+");
        }
        if (data.contains("%2F")) {
            data = data.replaceAll("%2F", "/");
        }
        byte[] datas;
        String value = null;
        try {
            if (System.getProperty("os.name") != null
                    && (System.getProperty("os.name").equalsIgnoreCase("sunos") || System
                    .getProperty("os.name").equalsIgnoreCase("linux"))) {
                datas = decode(Base64.decode(data, 0));
            } else {
                datas = decode(Base64.decode(data, 0));
            }
            value = new String(datas);
        } catch (Exception e) {
            value = "";
        }


        return value.trim();
    }


    /**
     * 获取字符串的MD5
     *
     * @param s 字符串
     * @return 成功则返回MD5结果，否则返回null
     */
    public static String getMD5(String s) {
        return EncryptCore.encryptMD5ToString(s);
    }
}
